#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>
#include <stdio.h>

extern void lonDiffRelay(tranDevice* trandevice);
extern void deltaDiffRelay(tranDevice* trandevice);
extern void zeroDiffRelay(tranDevice* trandevice);
extern void splitDiffRealy(tranDevice* trandevice);
extern void diffTrip(tranDevice* trandevice);
extern void lowDiffRelay(tranDevice* trandevice);

extern void impActh(tranDevice* trandevice);
extern void impActv(tranDevice* trandevice);
extern void impRelayh(tranDevice* trandevice);
extern void impRelayv(tranDevice* trandevice);
extern void zeroCurrh(tranDevice* trandevice);
extern void zeroCurrv(tranDevice* trandevice);

extern void compCurrh(tranDevice* trandevice);
extern void compCurrv(tranDevice* trandevice);
extern void compCurrl1(tranDevice* trandevice);
extern void compCurrl2(tranDevice* trandevice);



void tran500(tranDevice *trandevice){

    // 将采样值存入瞬时值数组
    tranSample2inst(trandevice);

    // 瞬时值滤波后存入并更新滤波后数组
    tranDataFilter(trandevice);

    // 利用滤波后数据计算12通道相量,存入phasor数组
    tranToPhasor(trandevice);

    tranHalfPhasor(trandevice);

    calIdmax(trandevice);
    phaseCalIdmax(trandevice);

    tranRecordMemory(trandevice);

    recordMemoryhalf(trandevice);

    tranHarmPhasor(trandevice);

    tranHalfInte(trandevice);

    tranDeltaCal(trandevice);
    tranPhaseDeltaCal(trandevice);

    //test mode
//    lonDiffRelay(trandevice);
/*
    diffTrip(trandevice);
    lonDiffRelay(trandevice);
    deltaDiffRelay(trandevice);
    zeroDiffRelay(trandevice);
    splitDiffRelay(trandevice);
    lowDiffRelay(trandevice);
*/

    if((trandevice->startEnable[6] == 1) && (trandevice->mainFlag == 0)) {
        if (trandevice->ptFlag == 0) {
            tranPTerror(trandevice);
        }
    }
    if((trandevice->startEnable[5] == 1) && (trandevice->mainFlag == 0)) {
        if(trandevice->ctFlag == 0){
            tranCTerror(trandevice);
        }
    }


    if ((trandevice->lonDiffStartFlag == 0) && (trandevice->startEnable[1] == 1) && (trandevice->mainFlag == 0)) {
        tranLonDiff(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->startEnable[0] == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        diffTrip(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        lonDiffRelay(trandevice);
    }

    if ((trandevice->deltaDiffStartFlag == 0) && ((trandevice->startEnable[1] == 1) || (trandevice->startEnable[6] == 1)) && (trandevice->mainFlag == 0)) {
        tranDeltaDiffStart(trandevice);
    }
    if ((trandevice->deltaDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        deltaDiffRelay(trandevice);
    }

    if ((trandevice->zeroDiffStartFlag == 0) && (trandevice->mainFlag == 0) && (trandevice->startEnable[1] == 1)){
        zeroStart(trandevice);
    }
    if ((trandevice->zeroDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        zeroDiffRelay(trandevice);
    }


    if ((trandevice->splitDiffStartFlag == 0) && (trandevice->mainFlag == 0)  && (trandevice->startEnable[4] == 1)){
        splitStart(trandevice);
    }
    if ((trandevice->splitDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        splitDiffRelay(trandevice);
    }


    if ((trandevice->lowDiffStartFlag == 0) && (trandevice->mainFlag == 0)  && (trandevice->startEnable[3] == 1)){
        lowDiffStart(trandevice);
    }
    if ((trandevice->lowDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        lowDiffRelay(trandevice);
    }


    if ((trandevice->phaseLonDiffStartFlag == 0) && (trandevice->mainFlag == 0) && (trandevice->startEnable[2] == 1)) {
        tranPhaseLonDiff(trandevice);
    }
    if ((trandevice->phaseLonDiffStartFlag == 1) && (trandevice->startEnable[0] == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        phaseDiffTrip(trandevice);
    }
    if ((trandevice->phaseLonDiffStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        phaseDiffRelay(trandevice);
    }


    if ((trandevice->phaseDeltaStartFlag == 0) && (trandevice->mainFlag == 0) && (trandevice->startEnable[2] == 1)) {
        tranPhaseDeltaStart(trandevice);
    }
    if ((trandevice->phaseDeltaStartFlag == 1) && (trandevice->mainFlag == 0) && (trandevice->ctFlag == 0)){
        phaseDeltaRelay(trandevice);
    }




    // 后备保护，根据需求投入
    if ((trandevice->impStartFlag_h == 0) && (trandevice->backEnable1[0] > 0) && (trandevice->mainFlag == 0)){
        impStarth(trandevice);
    }
    if ((trandevice->impStartFlag_h == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        impRelayh(trandevice);
    }

    if ((trandevice->impStartFlag_v == 0) && (trandevice->backEnable2[0] > 0) && (trandevice->mainFlag == 0)){
        impStartv(trandevice);
    }
    if ((trandevice->impStartFlag_v == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        impRelayv(trandevice);
    }

    if ((trandevice->zeroCurStartFlagh == 0) && (trandevice->backEnable1[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStarth(trandevice);
    }
    if ((trandevice->zeroCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrh(trandevice);
    }

    if ((trandevice->zeroCurStartFlagv == 0) && (trandevice->backEnable2[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStartv(trandevice);
    }
    if ((trandevice->zeroCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrv(trandevice);
    }

    if ((trandevice->zeroCurStartFlagt == 0) && (trandevice->backEnable3[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStartt(trandevice);
    }
    if ((trandevice->zeroCurStartFlagt == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrt(trandevice);
    }

    if ((trandevice->phaCurStartFlagh == 0) && (trandevice->backEnable1[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStarth(trandevice);
    }
    if ((trandevice->phaCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrh(trandevice);
    }

    if ((trandevice->phaCurStartFlagv == 0) && (trandevice->backEnable2[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartv(trandevice);
    }
    if ((trandevice->phaCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrv(trandevice);
    }

    if ((trandevice->phaCurStartFlagl1 == 0) && (trandevice->backEnable3[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartl1(trandevice);
    }
    if ((trandevice->phaCurStartFlagl1 == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrl1(trandevice);
    }

    if ((trandevice->phaCurStartFlagl2 == 0) && (trandevice->backEnable4[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartl2(trandevice);
    }
    if ((trandevice->phaCurStartFlagl2 == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrl2(trandevice);
    }


    if (trandevice->time < 0.2) {
        // 等待仿真进入稳定状态
        return;
    }

}



void tran220(tranDevice *trandevice){

    // 将采样值存入瞬时值数组
    tranSample2inst(trandevice);

    // 瞬时值滤波后存入并更新滤波后数组
    tranDataFilter(trandevice);

    // 利用滤波后数据计算12通道相量,存入phasor数组
    tranToPhasor(trandevice);

    tranHalfPhasor(trandevice);

    calIdmax(trandevice);

    tranRecordMemory(trandevice);

    recordMemoryhalf(trandevice);

    tranHarmPhasor(trandevice);

    tranDeltaCal(trandevice);

//    zeroCurrh(trandevice);
//    zeroCurrv(trandevice);

    tranHalfInte(trandevice);

    if((trandevice->startEnable[6] == 1) && (trandevice->mainFlag == 0)) {
        if (trandevice->ptFlag == 0) {
            tranPTerror(trandevice);
        }
    }
    if((trandevice->startEnable[5] == 1) && (trandevice->mainFlag == 0)) {
        if(trandevice->ctFlag == 0){
            tranCTerror(trandevice);
        }
    }


    if ((trandevice->lonDiffStartFlag == 0) && (trandevice->startEnable[1] == 1) && (trandevice->mainFlag == 0)) {
        tranLonDiff(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->startEnable[0] == 1) && (trandevice->mainFlag == 0)){
        diffTrip(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->mainFlag == 0)){
        lonDiffRelay(trandevice);
    }

    if ((trandevice->deltaDiffStartFlag == 0) && (trandevice->startEnable[1] == 1) && (trandevice->mainFlag == 0) ) {
        tranDeltaDiffStart(trandevice);
    }
    if ((trandevice->deltaDiffStartFlag == 1) && (trandevice->mainFlag == 0)){
        deltaDiffRelay(trandevice);
    }


    // 后备保护，根据需求投入

    if ((trandevice->impStartFlag_h == 0) && (trandevice->backEnable1[0] > 0) && (trandevice->mainFlag == 0)){
        impStarth(trandevice);
    }
    if ((trandevice->impStartFlag_h == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        impRelayh(trandevice);
    }

    if ((trandevice->impStartFlag_v == 0) && (trandevice->backEnable2[0] > 0) && (trandevice->mainFlag == 0)){
        impStartv(trandevice);
    }
    if ((trandevice->impStartFlag_v == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        impRelayv(trandevice);
    }
    if ((trandevice->zeroCurStartFlagh == 0) && (trandevice->backEnable1[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStarth(trandevice);
    }
    if ((trandevice->zeroCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrh(trandevice);
    }

    if ((trandevice->zeroCurStartFlagv == 0) && (trandevice->backEnable2[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStartv(trandevice);
    }
    if ((trandevice->zeroCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrv(trandevice);
    }


    if ((trandevice->phaCurStartFlagh == 0) && (trandevice->backEnable1[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStarth(trandevice);
    }
    if ((trandevice->phaCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrh(trandevice);
    }

    if ((trandevice->phaCurStartFlagv == 0) && (trandevice->backEnable2[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartv(trandevice);
    }
    if ((trandevice->phaCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrv(trandevice);
    }

    if ((trandevice->phaCurStartFlagl1 == 0) && (trandevice->backEnable3[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartl1(trandevice);
    }
    if ((trandevice->phaCurStartFlagl1 == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrl1(trandevice);
    }


    if (trandevice->time < 0.2) {
        // 等待仿真进入稳定状态
        return;
    }

}


void tran110(tranDevice *trandevice){

    // 将采样值存入瞬时值数组
    tranSample2inst(trandevice);

    // 瞬时值滤波后存入并更新滤波后数组
    tranDataFilter(trandevice);

    // 利用滤波后数据计算12通道相量,存入phasor数组
    tranToPhasor(trandevice);

    tranHalfPhasor(trandevice);

    calIdmax(trandevice);

    tranRecordMemory(trandevice);

    recordMemoryhalf(trandevice);

    tranHarmPhasor(trandevice);

    tranDeltaCal(trandevice);

//    zeroCurrh(trandevice);
//    zeroCurrv(trandevice);

    tranHalfInte(trandevice);

    if((trandevice->startEnable[6] == 1) && (trandevice->mainFlag == 0)) {
        if (trandevice->ptFlag == 0) {
            tranPTerror(trandevice);
        }
    }
    if((trandevice->startEnable[5] == 1) && (trandevice->mainFlag == 0)) {
        if(trandevice->ctFlag == 0){
            tranCTerror(trandevice);
        }
    }

    if ((trandevice->lonDiffStartFlag == 0) && (trandevice->startEnable[1] == 1) && (trandevice->mainFlag == 0)) {
        tranLonDiff(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->startEnable[0] == 1) && (trandevice->mainFlag == 0)){
        diffTrip(trandevice);
    }
    if ((trandevice->lonDiffStartFlag == 1) && (trandevice->mainFlag == 0)){
        lonDiffRelay(trandevice);
    }

    if ((trandevice->deltaDiffStartFlag == 0) && (trandevice->startEnable[1] == 1) && (trandevice->mainFlag == 0) ) {
        tranDeltaDiffStart(trandevice);
    }
    if ((trandevice->deltaDiffStartFlag == 1) && (trandevice->mainFlag == 0)){
        deltaDiffRelay(trandevice);
    }


    // 后备保护，根据需求投入
    if ((trandevice->zeroCurStartFlagh == 0) && (trandevice->backEnable1[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStarth(trandevice);
    }
    if ((trandevice->zeroCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrh(trandevice);
    }

    if ((trandevice->zeroCurStartFlagv == 0) && (trandevice->backEnable2[3] > 0) && (trandevice->mainFlag == 0)) {
        zeroCurStartv(trandevice);
    }
    if ((trandevice->zeroCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        zeroCurrv(trandevice);
    }

    if ((trandevice->phaCurStartFlagh == 0) && (trandevice->backEnable1[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStarth(trandevice);
    }
    if ((trandevice->phaCurStartFlagh == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrh(trandevice);
    }

    if ((trandevice->phaCurStartFlagv == 0) && (trandevice->backEnable2[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartv(trandevice);
    }
    if ((trandevice->phaCurStartFlagv == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrv(trandevice);
    }

    if ((trandevice->phaCurStartFlagl1 == 0) && (trandevice->backEnable3[2] > 0) && (trandevice->mainFlag == 0)) {
        phaseCurStartl1(trandevice);
    }
    if ((trandevice->phaCurStartFlagl1 == 1) && (trandevice->mainFlag == 0) && (trandevice->ptFlag == 0)){
        compCurrl1(trandevice);
    }


    if (trandevice->time < 0.2) {
        // 等待仿真进入稳定状态
        return;
    }

}